package vashion.azeroth.core.manager.personal;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.common.math.RandomUtil;
import vashion.azeroth.core.personal.pojo.SMSVarificationCode;
import vashion.azeroth.core.verificationcode.sms.ChuanglanSMSSender;

@Service
public class SMSVerificationCodeManager {

	private static final int VERIFICATION_CODE_LENGTH = 6;

	public static Map<String, SMSVarificationCode> verificationCodes = new HashMap<String, SMSVarificationCode>();

	@Autowired
	private ChuanglanSMSSender chuanglanSMSSender;

	/**
	 * 生成和发送验证码
	 * 
	 * @param mobilePhone
	 * @param purpose
	 * @return
	 */
	public boolean sendSMSVerificationCode(String mobilePhone, int purpose) {
		// 生成验证码
		SMSVarificationCode varificationCode = this.generateVerificationCode(mobilePhone, purpose);
		// 发送验证码短信
		boolean sendResult = this.sendSMS(varificationCode);
		// 放到缓存中
		if (sendResult) {
			verificationCodes.put(mobilePhone, varificationCode);
		}
		return sendResult;
	}

	/**
	 * 检查验证码
	 * 
	 * @param mobilePhone
	 * @param code
	 * @param purpose
	 * @return
	 */
	public int checkVerificationCode(String mobilePhone, String code, int purpose) {
		SMSVarificationCode varificationCode = verificationCodes.get(mobilePhone);
		// 验证码错误
		if (null == varificationCode || !code.equals(varificationCode.getVerificationCode())) {
			return 0;
		}
		// 验证码过期
		if ((new Date()).after(varificationCode.getExpiredDate())) {
			return 2;
		}
		// 验证码正确
		verificationCodes.remove(mobilePhone);
		return 1;
	}

	/**
	 * 生成验证码
	 * 
	 * @param mobilePhone
	 * @param purpose
	 * @return
	 */
	private SMSVarificationCode generateVerificationCode(String mobilePhone, int purpose) {
		Date now = new Date();
		SMSVarificationCode varificationCode = new SMSVarificationCode();
		varificationCode.setMobilePhone(mobilePhone);
		varificationCode.setPurpose(purpose);
		varificationCode.setVerificationCode(RandomUtil.getRandomNumr(VERIFICATION_CODE_LENGTH));
		varificationCode.setCreateDate(now);
		// 设置到期时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 10);// 让日期加1
		varificationCode.setExpiredDate(calendar.getTime());
		return varificationCode;
	}

	/**
	 * 发送验证码短信
	 * 
	 * @param varificationCode
	 * @return
	 */
	private boolean sendSMS(SMSVarificationCode varificationCode) {
		boolean result = false;
		// 短信内容
		String msg = "尊敬的客户，您的验证码为：" + varificationCode.getVerificationCode() + "，请在十分钟内输入。为保障您的账号安全，请勿将验证码短信转发给他人";
		String mobilePhone = varificationCode.getMobilePhone();
		try {
			if (isLocalPhone(mobilePhone)) {
				result = chuanglanSMSSender.sendVerificationCodeSMS(mobilePhone, msg);
			} else {
				result = chuanglanSMSSender.sendInternationVerificationCodeSMS(mobilePhone, msg);
			}
			return result;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 验证国内电话号码
	 * 
	 * @param str
	 * @return
	 */
	private static boolean isLocalPhone(String str) {
		Pattern p1 = null, p2 = null;
		Matcher m = null;
		boolean b = false;
		p1 = Pattern.compile("^[0]{1,2}[8]{1}[6]{1}[1]{1}[0-9]{10}$");  // 验证带区号的  
        p2 = Pattern.compile("^[1]{1}[0-9]{10}$"); // 验证没有区号的
		if (str.length() > 11) {
			m = p1.matcher(str);
			b = m.matches();
		} else {
			m = p2.matcher(str);
			b = m.matches();
		}
		return b;
	}
}
